<template>
  <div class="box-card">
    <div slot="header" class="cardHeader">
      <span class="headSpan">分类</span>
      <el-button class="headBtn" type="text" @click="handleAdd()">+ 新增</el-button>
    </div>
    <el-input
      v-model.trim="filterText"
      size="small"
      prefix-icon="el-icon-search"
      placeholder="请输入名称"
      maxlength="30"
      style="margin: 16px 0 0 0"
    />
    <div class="left-title" />
    <!-- <span class="alltext" @click="handleNodeClick({categoryId:''})">全部</span> -->
    <el-tree
      ref="tree"
      empty-text="暂无数据"
      :style="{ '--lightOpacity': lightTheme }"
      class="left-tree"
      :data="tree"
      :props="defaultProps"
      :default-expand-all="false"
      :expand-on-click-node="false"
      :highlight-current="true"
      :current-node-key="currentKey"
      :filter-node-method="filterNode"
      node-key="categoryId"
      :default-expanded-keys="expendData"
      @node-click="handleNodeClick"
    >
      <div slot-scope="{ data }" class="custom-tree-node">
        <DtOverTooltip :content="data.categoryName" />
        <template>
          <el-dropdown v-if="data.categoryId && data.categoryKey != 'lowcode'">
            <!-- <span class="el-dropdown-link">:</span> -->
            <span @click.stop>
              <svg-icon class="el-dropdown-link" icon-class="sandian" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(data)">
                编辑
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDel(data)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </div>
    </el-tree>
    <formTypeEdit ref="formTypeRef" :cascader-opts="tree" />
  </div>
</template>
<script>
import formTypeEdit from './formTypeEdit.vue'
import { categoryList, delCategory } from '@/framework/api/formDesign/formDesign'

export default {
  name: 'AdminGroup',
  components: { formTypeEdit },
  props: {
    name: {
      type: String,
      default: ''
    },
    handle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      selectedItem: '',
      orgName: '',
      filterText: '',
      initTreeData: [],
      tree: [],
      newTree: [],
      defaultProps: {
        children: 'children',
        label: 'categoryName'
      },
      currentKey: '',
      expendData: []
    }
  },
  computed: {
    lightTheme() {
      return `${this.$store.state.settings.theme}19`
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.initTree()
  },
  methods: {
    /**
       * 选中树节点
       */
    handleNodeClick(data) {
      this.selectedItem = data
      this.$emit('treeClick', data)
    },
    // 获取左侧组织树
    initTree(val) {
      categoryList().then((res) => {
        this.initTreeData = res.data
        this.initTreeData.unshift({ categoryId: '', categoryName: '全部' })
        this.tree = res.data
        if (!val) {
          this.selectedItem = res.data[0]
          // this.currentKey = res.data[0].categoryId
          this.expendData = [res.data[0].categoryId]
          this.$emit('treeClick', res.data[0])
          this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(res.data[0].categoryId)
          })
        } else {
          this.selectedItem = val
          // this.currentKey = val.categoryId
          this.expendData = [val.categoryId]
          this.$emit('treeClick', val)
          this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(val.categoryId)
          })
        }
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.categoryName.indexOf(value) !== -1
    },
    handleAdd() {
      this.$refs.formTypeRef.openOrg('', 'create', this.selectedItem)
    },
    handleEdit(data) {
      this.$refs.formTypeRef.openOrg(data, 'update')
    },
    handleDel(data) {
      this.$dtMessageBox({
        title: '提示',
        message: `确定要删除“${data.categoryName}”吗?`,
        type: 'warning'
      }).then(() => {
        delCategory({ categoryId: data.categoryId })
          .then((res) => {
            if (res.code === '00000') {
              if (this.selectedItem.categoryId === data.categoryId) {
                this.initTree()
              } else {
                this.initTree(this.selectedItem)
              }
              this.$dtMessage({
                message: '删除成功',
                type: 'success'
              })
            }
          })
          .catch(() => {
            // 取消不做操作
          })
      })
    }
  }
}
</script>
  <style lang="scss" scoped>
  .cardHeader {
    .headSpan {
      font-size: 16px;
      font-family: HarmonyOS Sans SC-Medium, HarmonyOS Sans SC;
      font-weight: 500;
      color: #131414;
      line-height: 24px;
    }

    .headBtn {
      float: right;
      border-radius: 4px 4px 4px 4px;
      opacity: 1;
      line-height: 24px;
      padding: 0;
      font-weight: 400;
    }
  }

  .svg-icon {
    font-size: 24px;
    display: block;
    margin-right: 8px !important;
    float: none !important;
  }

  .left-title {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 16px;
  }
  .alltext{
    cursor: pointer;
    padding: 0 20px;
    font-size: 14px;
  }
  .left-tree {
    margin-top: 23px;
  }

  ::v-deep .el-card__header {
    padding: 0;
    border-bottom: none;
  }

  ::v-deep .el-card__body {
    padding: 0;
  }

  ::v-deep .el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content {
    background-color: var(--lightOpacity);
    border-radius: 4px;
    color: var(--themeColor);

    .el-dropdown-link {
      color: var(--themeColor);
    }
  }

  ::v-deep .el-tree-node__content:hover,
  .el-upload-list__item:hover {
    background-color: var(--lightOpacity);
    border-radius: 4px;
    // color: var(--themeColor);
  }

  ::v-deep .el-tree-node__content {
    overflow: hidden;
    height: 38px;
  }

  ::v-deep .el-dropdown-menu__item {
    padding: 6px 16px !important;
    line-height: 24px;
  }

  .custom-tree-node {
    display: flex;
    align-items: center;
    overflow: hidden;
    width: 100%;

    .tree-name {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      flex: 1;
    }

    .btn {
      color: var(--themeColor);

      i {
        margin-right: 8px;
        font-size: 14px;
      }
    }

    .el-dropdown-link {
      margin-right: 8px;
      display: block;
      // width: 16px;
      // height: 16px;
      text-align: center;
      font-size: 14px;
    }

    .el-dropdown-link:hover {
      color: var(--themeColor);
    }
  }
  </style>

